/*
 * Copyright 2011-2012 by The Regents of the University of California Licensed
 * under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. you may obtain a copy of the
 * License from
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package edu.ucsd.db.bassarisk.query.binding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import edu.ucsd.db.bassarisk.query.binding.Bindings.Binding;

public class ActiveBindings {
  public static ActiveBindings createEmpty() {
    return new ActiveBindings();
  }

  private final Map<String, List<Binding>> internalMap;

  private ActiveBindings() {
    this.internalMap = new HashMap<String, List<Binding>>();
  }

  public void append(final Binding binding) {
    String runClassName = binding.boundSequenceClass().getName();
    if (!this.internalMap.containsKey(runClassName)) {
      this.internalMap.put(runClassName, new ArrayList<Binding>());
    }
    List<Binding> target = this.internalMap.get(runClassName);
    target.add(binding);
  }

  public void appendAll(final Iterable<Binding> iterable) {
    assert iterable.iterator().hasNext(); // size > 0
    for (Binding binding : iterable) {
      this.append(binding);
    }
  }

  public void clear(final String runClassName) {
    this.internalMap.remove(runClassName);
  }

  public List<Binding> getBindings(final String runClassName) {
    return this.internalMap.get(runClassName);
  }

  public Set<String> getBoundRunClassNames() {
    return new HashSet<String>(this.internalMap.keySet());
  }
}